const todolist = require('../models/todolist.js')

const getTodolist = async (ctx) => {
  const id = ctx.params.id
  const result = await todolist.getTodolistById(id)
  ctx.body = result
}

const createTodolist = async (ctx) => {
  const data = ctx.request.body
  const result = await todolist.createTodolist(data)
  if (result) {
    ctx.body = {
      success: true,
      message: '添加成功'
    }
  } else {
    ctx.body = {
      success: false,
      message: '添加失败'
    }
  }
}

const removeTodolist = async (ctx) => {
  const userId = ctx.params.user_id
  const id = ctx.params.id
  const result = await todolist.removeTodolist(userId, id)
  if (result) {
    ctx.body = {
      success: true,
      message: '删除成功'
    }
  } else {
    ctx.body = {
      success: false,
      message: '删除失败'
    }
  }
}

const updateTodolist = async (ctx) => {
  const userId = ctx.params.user_id
  const id = ctx.params.id
  let status = ctx.params.status
  status === '0' ? status = 1 : status = 0
  const result = await todolist.updateTodolist(userId, id, status)
  console.log('result:', result)
  if (result) {
    ctx.body = {
      success: true,
      message: '更新成功'
    }
  } else {
    ctx.body = {
      success: false,
      message: '更新失败'
    }
  }
  // todolist.updateTodolist(userId, id, status).then((result) => {
  //   console.log('result: ', result)
  //   if (result) {
  //     ctx.body = {
  //       success: true,
  //       message: '更新成功'
  //     }
  //   } else {
  //     ctx.body = {
  //       success: false,
  //       message: '更新失败'
  //     }
  //   }
  // }).catch((err) => {
  //   console.log(err)
  // })
}

module.exports = {
  getTodolist,
  createTodolist,
  removeTodolist,
  updateTodolist
}
